<template>
  <div class="dual_prevention_div" v-loading="tableLoading" element-loading-text="数据加载中">
    <div class="dual_prevention_query_div">
      <el-form :model="searchForm" :inline="true" size="small">
        <el-form-item label="" label-width="0">
          <!-- <el-input v-model="searchForm.riskUnitName" placeholder="请输入风险分析单元名称" clearable @clear="currentPage = 1;inquiry();"></el-input> -->
          <el-select v-model="searchForm.riskUnitName" placeholder="请选择风险分析对象" ref="objectSelect" style="width:90%;" @change="currentPage = 1;inquiry();">
            <el-option v-for="item in objectList" :key="item.oId" :label="item.oName" :value="item.oName" />
          </el-select>
        </el-form-item>
        <el-form-item label="" label-width="0">
          <el-input v-model="searchForm.hazardDep" placeholder="请输入责任部门" clearable @clear="currentPage = 1;inquiry();"></el-input>
        </el-form-item>
        <el-form-item label="" label-width="0">
          <el-select v-model="searchForm.ifStop" placeholder="请选择是否停运" clearable @change="currentPage = 1;inquiry();" @clear="currentPage = 1;inquiry();">
            <el-option label="正常" value="0" />
            <el-option label="停运" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="" label-width="0">
          <el-button type="success" size="small" @click="currentPage = 1;inquiry();">查询</el-button>
          <el-button type="primary" size="small" @click="handleAdd">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="dual_prevention_table_div">
      <el-table :data="tableData" style="width: 100%" height="100%">
        <el-table-column prop="riskUnitName" label="风险分析单元名称" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="hazardDep" label="责任部门" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="hazardLiablePerson" label="责任人" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="ifStop" label="是否停运" align="center">
          <template slot-scope="scope">
            {{ scope.row.ifStop === '0' ? '正常' : '停运' }}
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="400" fixed="right" align="center">
          <template slot-scope="scope">
            <CustomButton :onClick="() => handleEdit(scope.row)">编辑</CustomButton>
            <CustomButton :onClick="() => handleLook(scope.row)">详情</CustomButton>
            <CustomButton :onClick="() => handleBBInfo(scope.row)">生成包保数据</CustomButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dual_prevention_footer_div" v-show="pageLength * 1 > 0">
      <el-pagination @size-change="handleSizeChange" background @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizes" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="pageLength">
      </el-pagination>
    </div>
    <!-- 添加组件 -->
    <add-analysis ref="addAnalysis" @success="inquiry" />
    <!-- 编辑组件 -->
    <edit-analysis ref="editAnalysis" @success="inquiry" />
    <!-- 详情组件 -->
    <look-analysis ref="lookAnalysis" @success="inquiry" />
  </div>
</template>

<script>
import mixin from '@/mixins/index';
import CustomButton from '@/components/CustomButton/index.vue';
import AddAnalysis from './components/AddAnalysis.vue';
import EditAnalysis from './components/EditAnalysis.vue';
import LookAnalysis from './components/LookAnalysis.vue';
import { useRiskUnitApi } from '@/api/riskUnitApi';
import { useriskObjectApi } from '@/api/riskObjectApi';
export default {
  mixins: [mixin],
  components: {
    CustomButton,
    AddAnalysis,
    EditAnalysis,
    LookAnalysis
  },
  data() {
    return {
      tableLoading: false,
      tableData: [],
      objectList: [],

      searchForm: {
        riskUnitName: '',
        hazardDep: '',
        ifStop: ''
      }
    };
  },
  created() {
    this.inquiry();
    this.getObjectList();
  },
  methods: {
    inquiry() {
      this.tableLoading = true;
      const params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm
      };
      useRiskUnitApi()
        .findUnit(params)
        .then((resp) => {
          if (resp.code * 1 === 100) {
            this.tableData = resp.info.records;
            this.pageLength = resp.info.total;
          } else {
            this.$message.error(resp.message || '数据请求失败');
          }
          this.tableLoading = false;
        })
        .catch((err) => {
          console.error('查询失败:', err);
          this.$message.error(err.message || '请求发生错误');
          this.tableLoading = false;
        });
    },
    handleAdd() {
      this.$refs.addAnalysis.show();
    },
    handleEdit(row) {
      this.$refs.editAnalysis.show(row);
    },
    handleLook(row) {
      this.$refs.lookAnalysis.show(row);
    },
    handleBBInfo(row) {
      this.$confirm('确认生成包保数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        useRiskUnitApi()
          .addUnitBBInfo({ id: row.id })
          .then((res) => {
            if (res.code * 1 === 100) {
              this.$message.success('生成成功');
              this.inquiry();
            } else {
              this.$message.error(res.message || '生成失败');
            }
          })
          .catch((err) => {
            console.error('生成失败:', err);
            this.$message.error('生成失败，请稍后重试');
          });
      });
    },
    getObjectList() {
      useriskObjectApi()
        .findObject()
        .then((res) => {
          if (res.code * 1 === 100) {
            this.objectList = res.info.records || [];
          }
        });
    }
  }
};
</script>
<style lang="scss" scoped>
@import '@/theme/scroll-bar.scss';

.dual_prevention_div {
  // background-color: red;
  // max-height: fit-content;
  height: calc(100vh - 120px);
  display: flex;
  background-color: #ffffff;
  flex-direction: column;
  flex: 1;
  width: 100%;
  border: 1px solid #ebeef5;
  color: #303133;
  padding: 15px;

  .dual_prevention_query_div {
    width: 100%;
  }

  .dual_prevention_table_div {
    flex: 1;
    overflow: hidden;
    width: 100%;

    .el-table {
      flex: 1;
    }

    .el-table__inner-wrapper {
      position: relative;
      display: flex;
      flex-direction: column;
      height: 100%;
      flex-wrap: nowrap;
    }
  }

  .dual_prevention_footer_div {
    margin-top: 15px !important;
    width: 100%;
    height: 24px;
  }
}

.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  // padding: 0  ;/
}
</style>
